package org.example.chapter1;

import java.util.concurrent.Callable;
import java.util.concurrent.FutureTask;

/**
 * 第一章 1.2 线程创建与运行
 * 3种创建线程的方法
 */
public class S12_CreateAndRunThread {

    public static void main(String[] args) {
        S12_CreateAndRunThread creator = new S12_CreateAndRunThread();
        creator.createMethod1();
        creator.createMethod2();
        creator.createMethod3();
    }

    public void createMethod1() {
        new Thread() {
            @Override
            public void run() {
                System.out.println(threadId() + ": Thread1 run");
            }
        }.start();
    }

    public void createMethod2() {
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                System.out.println(Thread.currentThread().threadId() + ": Thread2 run");
            }
        };
        new Thread(runnable).start();
    }

    public void createMethod3() {
        FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                return Thread.currentThread().threadId() + ": Thread3 run";
            }
        });
        new Thread(futureTask).start();
        try {
            System.out.println(futureTask.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
